//
//  FirstViewController.m
//  HGY
//
//  Created by lanou3g on 15/10/27.
//  Copyright (c) 2015年 顾成辉. All rights reserved.
//

#import "FirstViewController.h"

// #define ee 4
@interface FirstViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, ChengShiVCDelegate>
@property (nonatomic, retain)NSMutableArray *dataArray;

@end

@implementation FirstViewController




- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   //  [self size];
    // Do any additional setup after loading the view.
   // self.view.backgroundColor = [UIColor grayColor];
   // fristView *view = [[fristView alloc] init];
    //[self.view addSubview:view];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [[UIColor orangeColor]colorWithAlphaComponent:0.5];
    self.navigationController.navigationBar.alpha = 0.5;
    [[firstFootManager shareManager] requestDataForReloadWithUrl:firstUrl Block:^(NSArray *array) {
         self.dataArray =(NSMutableArray *) array;

           [self.tableView reloadData];
        [self addtableView];
        
    }];
    [self addBarButton];

   // [self.tableView reloadData];
    
}


- (void)addBarButton
{
    if (self.cityString == nil) {
        self.cityString = @"上海";
    }
    NSLog(@"---%@", self.cityString);
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:self.cityString style:(UIBarButtonItemStylePlain) target:self action:@selector(addBarButton:)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)addBarButton:(UIBarButtonItem *)button
{
    ChengShiVC *vc = [[ChengShiVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.delegate = self;
}

- (void)string:(NSString *)str
{
    self.cityString = str;
    NSLog(@"------------%@", str);
    NSLog(@"***********%@", self.cityString);
    [self addBarButton];
}



#pragma  mark -- tableView
- (void)addtableView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeMake(0, 90);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.headerReferenceSize = CGSizeMake(0, 20);

    layout.sectionInset = UIEdgeInsetsMake(5 * kWidth, 5 * kHeight, 5 * kWidth , 5 * kHeight );
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake (0, 0, kScreenWidth, (kScreenHeight - 108)  ) collectionViewLayout:layout];

    layout.itemSize = CGSizeMake(100 * kWidth, 130 * kHeight);
   //  layout.sectionInset = UIEdgeInsetsMake(5 , 5 , 5  , 5  );
//    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake (0, 0, kScreenWidth, (kScreenHeight - 108)  ) collectionViewLayout:layout];

    collectionView.delegate = self;
    collectionView.dataSource = self;
    //collectionView.bounces = NO;
    [self.view addSubview:collectionView];
    collectionView.backgroundColor = [[UIColor orangeColor]colorWithAlphaComponent:0.3];
    
    
    [collectionView registerClass:[fristCollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];
    

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return 12;
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    fristCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        Model *model = [[firstFootManager shareManager] Model];
        cell.imageV.image = [UIImage imageNamed:@"MeiShi.jpg"];
        cell.titleLabel.text = model.category_name;
       // cell.backgroundColor = [[UIColor orangeColor]colorWithAlphaComponent:0.3];
    }
    

    if (indexPath.section == 1) {
        Model *model = [[firstFootManager shareManager] ModelArray:indexPath.row ];
        cell.titleLabel.text = model.category_name;

//        Model *model = [[firstFootManager shareManager] ModelArray:indexPath.row ];
//        cell.titleLabel.text = model.category_name;

        NSArray *array = @[@"QinZi.jpg", @"JiaZhuang.jpg", @"JingPinGou.jpg", @"JiuDian.jpg", @"XiuXianYuLe.jpg", @"ShengHuoFuWu.jpg", @"LiRen.jpg", @"YaChi.jpg", @"LvYou.jpg", @"GouWu.jpg", @"DianYing.jpg", @"JieHun.jpg"];
        cell.imageV.image = [UIImage imageNamed:array[indexPath.row]];
        
       // cell.backgroundColor = [[UIColor clearColor]colorWithAlphaComponent:0.3];
        
    }
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    return CGSizeMake(100 * kWidth, 130 * kHeight);


}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    fristVC *vc = [[fristVC alloc] init];
    if (indexPath.section == 1) {
        Model *model = [[firstFootManager shareManager] ModelArray:indexPath.row ];
        vc.string = model.category_name;
        vc.cityStr = self.cityString;
    }
    if (indexPath.section == 0) {
        Model *model = [[firstFootManager shareManager] Model];
        vc.string = model.category_name;
        vc.cityStr = self.cityString;
    }
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
